using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

using Autodesk.Connectivity.WebServices;
using Autodesk.Connectivity.WebServicesTools;
//using JustOnesAndZeros.WebServices.Util;

namespace EffectiveFolderPermissions
{

    [XmlRoot("SETTINGS")]
    public class Settings
    {

        private static string SETTINGS_VAULT_OPTION_NAME = "Autodesk.EffectiveFolderPermissions.Settings";

        [XmlElement("INCLUDE_FOLDER")]
        public List<String> IncludedFolders;

        [XmlElement("EXCLUDE_FOLDER")]
        public List<String> ExcludedFolders;

        [XmlAttribute("FOLDER_DEPTH")]
        public int FolderDepth;

        [XmlAttribute("SHOW_INACTIVE_USERS")]
        public bool ShowInactiveUsers;

        [XmlAttribute("SHOW_USERS_WITHOUT_VAULT_ACCESS")]
        public bool ShowUsersWithoutVaultAccess;

        public Settings()
        {
            ExcludedFolders = new List<string>();
            IncludedFolders = new List<string>();
            //webServiceManager = wsm;
        }

        public void SaveSettings(WebServiceManager wsm)
        {
            KnowledgeVaultService kvSvc = wsm.KnowledgeVaultService;// (KnowledgeVaultService)ServiceManager.GetService("KnowledgeVaultService");

            string settingsString = null;

            try
            {
                using (System.IO.StringWriter writer = new System.IO.StringWriter())
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    serializer.Serialize(writer, this);
                    settingsString = writer.ToString();
                }
            }
            catch
            { }

            kvSvc.SetVaultOption(SETTINGS_VAULT_OPTION_NAME, settingsString);
        }

        public static Settings LoadSettings(WebServiceManager wsm)
        {
            Settings retval = null;

            KnowledgeVaultService kvSvc = wsm.KnowledgeVaultService;//(KnowledgeVaultService)ServiceManager.GetService("KnowledgeVaultService");

            string settingsString = kvSvc.GetVaultOption(SETTINGS_VAULT_OPTION_NAME);
            if (settingsString != null && settingsString.Length > 0)
            {
                try
                {
                    using (System.IO.StringReader reader = new System.IO.StringReader(settingsString))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(Settings));

                        retval = (Settings)serializer.Deserialize(reader);
                    }
                }
                catch
                { }
            }

            if (retval == null)
            {
                retval = new Settings()
                {
                    FolderDepth = 2,
                    ShowInactiveUsers = false,
                    ShowUsersWithoutVaultAccess = false,
                };
            }

            return retval;
        }
    }
}
